package com.example.springboot.controller;

import com.example.springboot.entity.User;
import io.swagger.annotations.ApiModelProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UserController extends BasicController{

    protected Logger logger = LoggerFactory.getLogger(BasicController.class);

    @PostMapping("/signup")
    public User signUp(@RequestBody User user) {
        User bizUser = userResponsitory.findByUsername(user.getUsername());
        if (null != bizUser) {
            throw new RuntimeException("用户已经存在");
        }
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        User save = userResponsitory.save(user);
        return save;
    }

    /**
     * 获取用户列表
     * @return
     */
    @ApiModelProperty(value = "获取用户列表")
    @GetMapping("/userList")
    public Map<String, Object> userList(){
        List<User> users = userResponsitory.findAll();
        logger.info("users: {}", users);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("users",users);
        return map;
    }

    /**
     * 获取用户权限
     * @return
     */
    @ApiModelProperty(value = "获取用户权限")
    @GetMapping("/authorityList")
    public List<String> authorityList(){
        List<String> authentication = getAuthentication();
        return authentication;
    }

}
